// JavaScript Document
;
function Ctrace(style_top){
	//盒子的id
    this.boxid=Ctrace.mkId('_BOX');	
	//计录所有trace层的id
	this.traceid=new Array();
	if(typeof style_top ==='undefined' ){
	    this.style_top = 'top:0px;';
	}else{
		this.style_top=style_top;
	}
	
	this.mkBox()
	this.traceBgColor = '#f60'
	this.traceFontSize = 48 ;
	this.currentTID = false ;
	//计算count的次数 
	this.count = 0;
	this.timeoutStep = 800;
	this.timeoutTime = 0;
	this.isKillTrace = true ;
	this.tTimeOut=4000;
	
}
//生成一个碓一的随机id
Ctrace.mkId=function(s){
	if(typeof s === 'undefined'){
	    s = '';	
	}
 return 'ID_' + (new Date()).getTime() +'_'
        +    Math.round(  Math.random()*( 999999-1 - 100000) + 100000  )
		+    Math.round(  Math.random()*( 999999-1 - 100000) + 100000  )
		+s ;	
}

//创建trace层
Ctrace.prototype.mkTraceDiv=function(msg,id,boxid,traceBgColor,size,isRemove){
 var oDiv = document.createElement('div');
    oDiv.innerHTML = msg;
    oDiv.id = id;
    oDiv.style.cssText = 'z-index:99;color:#fff; background-color:'+traceBgColor+';'
        + 'width:'+(window.innerWidth  )+'px;' +' font-size:'+size+'px;margin-bottom:5px;'
        +' border-radius: 5px;filter: alpha(opacity = 80);opacity: 0.8; margin:0px;';
	oDiv.setAttribute('onDblClick','this.style.display="none"')
    document.getElementById( boxid ).appendChild(oDiv)
	if(this.isKillTrace){
		if(isRemove){
			console.log(this.currentTID);
	   window.setTimeout((function(){oDiv.parentNode.removeChild(oDiv ); this.currentTID = false;
		 
			}).__$bind$__(this),this.tTimeOut)	
		}else{
		window.setTimeout((function(){oDiv.parentNode.removeChild(oDiv ); this.currentTID = false;
			}).__$bind$__(this),this.tTimeOut)	
		}
	}else{
		
	}
}

//创建一个盒子，用来装 trace层
Ctrace.prototype.mkBox=function(){
   // if ( ! document.getElementById(this.boxid)) {
		//创建一装弹出杠的盒子，
		//如果创建了就不再创建
        var box = document.createElement('div')
        box.id = this.boxid;
		box.setAttribute('style',"position:fixed;z-index:90;width:"+window.innerWidth+'px;'+this.style_top)
       // box.style.cssText = 'position:fixed;z-index:99;top:0px;'
        window.document.getElementsByTagName('body')[0].appendChild(box)
   // }	
}

Ctrace.prototype.trace=function (msg,traceBgColor , fontsize,isRemove) {
	 var bgc;
     if(typeof traceBgColor === 'undefined' || ''=== traceBgColor){
	    bgc = this.traceBgColor 	 
	 }else{
	    bgc =  traceBgColor 
	 }
	 
	  var size;
     if(typeof fontsize === 'undefined' ){
	    size = this.traceFontSize 	 
	 }else{
	    size =  fontsize 
	 }
	 if(typeof isRemove === 'undefined' ){
	    isRemove = true 	 
	 }else{
	    isRemove =  false 
	 }
	 
	//创建一个弹出框
	var oDivid=Ctrace.mkId('_Trace');
	this.traceid.push(oDivid);	
	this.currentTID = oDivid
	
	this.mkTraceDiv(msg,oDivid ,this.boxid,bgc ,size,isRemove)	
	window.addEventListener('resize',function(){	
	       //console.warn( document.getElementById(oDivid));
		   if(document.getElementById(oDivid)){	
		    document.getElementById(oDivid).style.width = (window.innerWidth )+'px';	
		   }
		} , false)
}

Ctrace.prototype.traceOne=function(msg,isSetTimeOut){
	
	if(this.currentTID === false){
	    this.trace(msg,this.traceBgColor,this.traceFontSize,false)	
		 
	}
	
	if(typeof isSetTimeOut === 'undefined'){
	    isSetTimeOut = true;	
	}
	this.count++;
	this.timeoutTime += this.timeoutStep 
	var _count =  this.count
	if(this.isKillTrace && this.currentTID !== false){
	    var _currentTID = this.currentTID
	}
	 if(isSetTimeOut && this.isKillTrace){
		 
		try{ 
	        window.setTimeout(function(){
	        document.getElementById(_currentTID).innerHTML=_count+'.'+msg;	
		    } ,this.timeoutTime)//this.count*this.timeoutStep	 
		 }catch(e){
		this.currentTID=false;
		this.traceOne (msg,isSetTimeOut)	 
		 }
		
	 }else{
		 try{
	    document.getElementById(this.currentTID).innerHTML=this.count+'.'+msg;
		 }catch(e){
		//this.currentTID=false;
		    this.traceOne (msg,isSetTimeOut)	 
		 }
	 }
	
}
Ctrace.prototype.step=function(time){
    this.timeoutStep =time;	
}
//END